﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Admin
{
    public partial class adminContactPersonCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.Form.DefaultButton = btnAdminCreateContactPerson.UniqueID;

                BindSubMenu();
                lblAdminCreateContactPersonSucces.Visible = false;

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkAdminDashboard.NavigateUrl = "~/Admin/admin.aspx";
            hyLinkUserHandling.NavigateUrl = "~/Admin/adminUsers.aspx";
            hyLinkAdminExchange.NavigateUrl = "~/Admin/adminExchange.aspx";
            hyLinkNewsHandling.NavigateUrl = "~/Admin/adminNews.aspx";
            hyLinkEditInformation.NavigateUrl = "~/Admin/adminInformation.aspx";
            hyLinkResetUsers.NavigateUrl = "~/Admin/adminUsersReset.aspx";
            hyLinkCreateContactPerson.NavigateUrl = "~/Admin/adminContactPersonCreate.aspx";
            hyLinkContactPerson.NavigateUrl = "~/Admin/adminContactPerson.aspx";
            hylinkDepartment.NavigateUrl = "~/Admin/adminDepartment.aspx";
        }

        /// <summary>
        /// Creates a contact person, based on the input parameters.
        /// </summary>
        /// <param name="fName">Specifies the first name to be used.</param>
        /// <param name="lName">Specifies the last name to be used.</param>
        /// <param name="hasImage">Specifies whether or not contact person has an image.</param>
        /// <param name="imageFileName">Specifies the file name of the image to be used.</param>
        /// <param name="phone">Specifies the phone to be used in the contact person.</param>
        /// <param name="email">Specifies the email to be used in the contact person.</param>
        /// <param name="title">Specifies the title to be used in the contact person.</param>
        /// <param name="department">Specifies the department to be used in the contact person</param>
        private void CreateContactPerson(string fName, string lName, bool hasImage, string imageFileName, string phone, string email, string title, string department)
        {
            dbAdmin.CreateContactPerson(fName, lName, hasImage, imageFileName, phone, email, title, department);
        }

        /// <summary>
        /// Uploads an image.
        /// </summary>
        private void UploadPicture()
        {
            string path = Server.MapPath("~/Images/ContactPersons/" + txtCreateContactPersonFName.Text.Replace(" ", "") + "_" + txtCreateContactPersonLName.Text.Trim() + "/");
            if (!Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);

                dir.Create();
            }

            if (fuCreateContactPersonImage.HasFile)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(fuCreateContactPersonImage.FileContent);
                string imagePath = path + fuCreateContactPersonImage.FileName;

                GeneralTools.CreateThumbnail(100, image, imagePath);
            }
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            hyLinkAdminDashboard.Text = GetLocalResourceObject("hyLinkAdminDashboardText").ToString();
            hyLinkUserHandling.Text = GetLocalResourceObject("hyLinkUserHandlingText").ToString();
            hyLinkAdminExchange.Text = GetLocalResourceObject("hyLinkAdminExchangeText").ToString();
            hyLinkNewsHandling.Text = GetLocalResourceObject("hyLinkNewsHandlingText").ToString();
            hyLinkEditInformation.Text = GetLocalResourceObject("hyLinkEditInformationText").ToString();
            hyLinkResetUsers.Text = GetLocalResourceObject("hyLinkResetUsersText").ToString();
            hyLinkCreateContactPerson.Text = GetLocalResourceObject("hyLinkCreateContactPersonText").ToString();
            hyLinkContactPerson.Text = GetLocalResourceObject("hyLinkContactPersonText").ToString();
            hylinkDepartment.Text = GetLocalResourceObject("hylinkDepartmentText").ToString();

            lblAdminContentHeader.Text = GetLocalResourceObject("lblAdminContentHeaderText").ToString();
            lblAdminCreateContactPersonSucces.Text = GetLocalResourceObject("lblAdminCreateContactPersonSuccesText").ToString();
            lblCreateContactPersonImage.Text = GetLocalResourceObject("lblCreateContactPersonImageText").ToString();
            lblCreateContactPersonFName.Text = GetLocalResourceObject("lblCreateContactPersonFNameText").ToString();
            lblCreateContactPersonLName.Text = GetLocalResourceObject("lblCreateContactPersonLNameText").ToString();
            lblCreateContactPersonPhone.Text = GetLocalResourceObject("lblCreateContactPersonPhoneText").ToString();
            lblCreateContactPersonTitle.Text = GetLocalResourceObject("lblCreateContactPersonTitleText").ToString();
            lblCreateContactPersonDepartment.Text = GetLocalResourceObject("lblCreateContactPersonDepartmentText").ToString();
            lblCreateContactPersonEmail.Text = GetLocalResourceObject("lblCreateContactPersonEmailText").ToString();

            btnAdminCreateContactPerson.Text = GetLocalResourceObject("btnAdminCreateContactPersonText").ToString();

            string imageRegexValidaton = GetLocalResourceObject("regexvalContactPersonImageText").ToString();
            regexvalContactPersonImage.ToolTip = imageRegexValidaton;
            regexvalContactPersonImage.ErrorMessage = imageRegexValidaton;

            string phoneRegexValidation = GetLocalResourceObject("regexvalPhoneText").ToString();
            regexvalPhone.ToolTip = phoneRegexValidation;
            regexvalPhone.ErrorMessage = phoneRegexValidation;

            string EmailRegexValidation = GetLocalResourceObject("regexvalEmailText").ToString();
            regexvalEmail.ToolTip = EmailRegexValidation;
            regexvalEmail.ErrorMessage = EmailRegexValidation;

            string fNameValidation = GetLocalResourceObject("rfvalFnameText").ToString();
            rfvalFname.ToolTip = fNameValidation;
            rfvalFname.ErrorMessage = fNameValidation;

            string lNameValidation = GetLocalResourceObject("rfvalLNameText").ToString();
            rfvalLName.ToolTip = lNameValidation;
            rfvalLName.ErrorMessage = lNameValidation;

            string emailValidation = GetLocalResourceObject("rfvalEmailText").ToString();
            rfvalEmail.ToolTip = emailValidation;
            rfvalEmail.ErrorMessage = emailValidation;

            string TitleValidation = GetLocalResourceObject("rfvalTitleText").ToString();
            rfvalTitle.ToolTip = TitleValidation;
            rfvalTitle.ErrorMessage = TitleValidation;

            string DepartmentValidation = GetLocalResourceObject("rfvalDepartmentText").ToString();
            rfvalDepartment.ToolTip = DepartmentValidation;
            rfvalDepartment.ErrorMessage = DepartmentValidation;

            valSumAdminCreateContactPerson.HeaderText = GetLocalResourceObject("valSumAdminCreateContactPersonText").ToString();
        }

        protected void btnAdminCreateContactPerson_Click(object sender, EventArgs e)
        {
            string fName = txtCreateContactPersonFName.Text;
            string lName = txtCreateContactPersonLName.Text;
            bool hasImage = fuCreateContactPersonImage.HasFile;
            string imageFileName = fuCreateContactPersonImage.FileName;
            string phone = txtCreateContactPersonPhone.Text;
            string email = txtCreateContactPersonEmail.Text;
            string title = txtCreateContactPersonTitle.Text;
            string department = txtCreateContactPersonDepartment.Text;

            //if (HttpContext.Current.Session["MyCulture"] != null)
            //    culture = HttpContext.Current.Session["MyCulture"].ToString();

            UploadPicture();
            CreateContactPerson(fName, lName, hasImage, imageFileName, phone, email, title, department);
            panAdminCreateContactPerson.Visible = false;
            lblAdminCreateContactPersonSucces.Visible = true;
        }
    }
}